package com.dempsey.rabbitmqproducer.service;

import com.alibaba.fastjson.JSON;
import com.dempsey.rabbitmqproducer.domain.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * The type Order service.
 *
 * @author Dempsey  Date 2022/4/14 Description
 */
@Service
public class OrderService {

    @Autowired
    RabbitTemplate rabbitTemplate;


    /**
     * Make order.
     * 模拟用户下单
     *
     * @param userId    the user id
     * @param productId the product id
     * @param num       the num
     */
    public void makeOrder(String userId, String productId, int num) {
        // 1、根据商品id查询库存是否充足

        // 2、保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功");
        // 3、通过mq实现消息分发
        // 参数1: 交换机  参数2: 路由key/queue队列名 参数3: 消息内容
        String exchangeName = "fanout_order_exchange";
        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName, routingKey, JSON.toJSONString(new User(1,"name")));

    }

}
